import Vue from 'vue'
import Vuex from 'vuex'
import { setItem, getItem } from '@/utils/storage'

Vue.use(Vuex)

const TOKEN_KEY = 'TOUTIAO_USER'
export default new Vuex.Store({
  state: {
  // 因为本地为字符串,所以需要转为对象
    user: getItem(TOKEN_KEY)
    // user: JSON.parse(window.sessionStorage.getItem(TOKEN_KEY))
    // user: null
  },
  mutations: {
    setUser (state, data) {
      state.user = data
      // 为了防止刷新容器中的token就消失，需要备份到本地格式为字符串
      setItem(TOKEN_KEY, state.user)
      // window.sessionStorage.setItem(TOKEN_KEY, JSON.stringify(state.user))
    }
  },
  actions: {
  },
  modules: {
  }
})
